import { createSlice } from "@reduxjs/toolkit";

const counterStore = createSlice({
    // 模块名称独一无二
    name: 'counter',
    // 初始数据
    initialState: {
        count: 1
    },
    // 修改数据的同步方法,支持直接修改
    reducers: {
        increment(state) {
            state.count++
        },
        decrement(state) {
            state.count--
        },
        addToNum(state, action) {
            state.count = action.payload
        }
    }
})
// 解构出actionCreater函数
const { increment, decrement ,addToNum} = counterStore.actions
// 获取reducer函数
const counterReducer = counterStore.reducer

//以按需导出的方式导出actionCreate函数
export { increment, decrement ,addToNum}

//以默认导出的方式导出reducer
export default counterReducer
